package com.aizhixin.lab.course.classes.controller;

import com.aizhixin.lab.account.dto.AccountDTO;
import com.aizhixin.lab.account.service.AccountService;
import com.aizhixin.lab.common.core.ApiReturnConstants;
import com.aizhixin.lab.common.core.UserInforCore;
import com.aizhixin.lab.common.utils.PageJdbcUtil;
import com.aizhixin.lab.common.utils.TokenUtil;
import com.aizhixin.lab.company.domain.SystemSetDomain;
import com.aizhixin.lab.company.service.SystemSetService;
import com.aizhixin.lab.course.classes.domain.*;
import com.aizhixin.lab.course.classes.dto.CourseClassDTO;
import com.aizhixin.lab.course.classes.entity.CourseClass;
import com.aizhixin.lab.course.classes.entity.LabReport;
import com.aizhixin.lab.course.classes.service.CourseClassService;
import com.aizhixin.lab.course.classes.service.LabReportService;
import com.aizhixin.lab.course.course.entity.Course;
import com.aizhixin.lab.course.course.entity.CourseChapter;
import com.aizhixin.lab.course.course.service.CourseService;
import com.aizhixin.lab.course.course.service.TeacherChapterService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api/v1/course/labreport")
@Api(value = "实验报告相关API")
public class LabReportController {
	@Autowired
	private AccountService accountService;
	@Autowired
	private LabReportService labReportService;
	@Autowired
	private CourseService courseService;
	@Autowired
	private TeacherChapterService teacherChapterService;
	@Autowired
	private CourseClassService courseClassService;

	@RequestMapping(value = "/iscommit", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
	@ApiOperation(httpMethod = "GET", value = "当前选修班是否有提交过实验报告（true-提交过，false-未提交），修改选修班学生时会清空学生列表重新添加，这时若存在提交的实验报告需要提示用户，若未提交过则不提示用户", response = Void.class, notes = "选修班是否提交过实验报告<br>@author zhengning")
	public ResponseEntity<?> isCommit(
			@RequestHeader("Authorization") String token,
			@ApiParam(value = "courseClassId课程班级id", required = false) @RequestParam(value = "courseClassId", required = true) String courseClassId) {
		AccountDTO account = accountService.getUserByToken(token);
		if (account == null) {
			return new ResponseEntity<Object>(TokenUtil.tokenValid(),
					HttpStatus.UNAUTHORIZED);
		}
		Map<String, Object> result = new HashMap<String, Object>();
		long countNum = labReportService
				.countCommitNumByCourseClassId(courseClassId);
		if (countNum > 0) {
			result.put(ApiReturnConstants.DATA, false);
		} else {
			result.put(ApiReturnConstants.DATA, true);
		}
		result.put(ApiReturnConstants.SUCCESS, true);
		return new ResponseEntity(result, HttpStatus.OK);
	}

	@RequestMapping(value = "/statistics", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
	@ApiOperation(httpMethod = "GET", value = "教师个人中心统计（courseNum-从课程中心引入课程数量,reportNum-待审核实验报告数量）学生个人中心统计（courseNum-参加课程数量,reportNum-未提交/待审核/通过/未通过实验报告数量）", response = Void.class, notes = "教师个人中心统计<br>@author zhengning")
	public ResponseEntity<?> statistics(
			@RequestHeader("Authorization") String token) {
		AccountDTO account = accountService.getUserByToken(token);
		if (account == null) {
			return new ResponseEntity<Object>(TokenUtil.tokenValid(),
					HttpStatus.UNAUTHORIZED);
		}
		Map<String, Object> result = null;
		if (UserInforCore.ROLE_TEACHER.equals(account.getRole())) {
			result = labReportService.teacherStatistics(account.getId());
		} else {
			result = labReportService.stuStatistics(account.getId());
		}
		return new ResponseEntity(result, HttpStatus.OK);
	}

	@RequestMapping(value = "/getonereport", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
	@ApiOperation(httpMethod = "GET", value = "学生查询某个章节下实验报告", response = Void.class, notes = "学生查询某个章节下实验报告<br>@author zhengning")
	public ResponseEntity<?> getOneReport(
			@RequestHeader("Authorization") String token,
			@ApiParam(value = "chaperId章节id", required = false) @RequestParam(value = "chaperId", required = true) Long chaperId) {
		AccountDTO account = accountService.getUserByToken(token);
		if (account == null) {
			return new ResponseEntity<Object>(TokenUtil.tokenValid(),
					HttpStatus.UNAUTHORIZED);
		}
		Map<String, Object> result = labReportService.getStuReport(
				account.getId(), chaperId);

		return new ResponseEntity(result, HttpStatus.OK);
	}
	@RequestMapping(value = "/testClassReport", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
	@ApiOperation(httpMethod = "POST", value = "初始化班级实验报告", response = Void.class, notes = "初始化班级实验报告<br>@author lwq")
	public ResponseEntity<?> testClassReport(
			@RequestHeader("Authorization") String token,
			@ApiParam(value = "chaperId章节id", required = false) @RequestBody ArrayList<CourseClass> reports) {
		AccountDTO account = accountService.getUserByToken(token);
		if (account == null) {
			return new ResponseEntity<Object>(TokenUtil.tokenValid(),
					HttpStatus.UNAUTHORIZED);
		}
		List<LabReport> labReports = labReportService.initClassLabReport(reports);

		return new ResponseEntity(labReports, HttpStatus.OK);
	}

	@RequestMapping(value = "/save", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
	@ApiOperation(httpMethod = "POST", value = "保存实验报告", response = Void.class, notes = "保存实验报告<br>@author zhengning")
	public ResponseEntity<?> save(
			@RequestHeader("Authorization") String token,
			@ApiParam(value = "id和content为必填项", required = false) @RequestBody LabReportDomain domain) {
		AccountDTO account = accountService.getUserByToken(token);
		if (account == null) {
			return new ResponseEntity<Object>(TokenUtil.tokenValid(),
					HttpStatus.UNAUTHORIZED);
		}
		Map<String, Object> result = null;
		if (StringUtils.isEmpty(domain.getId())) {
			result = new HashMap();
			result.put(ApiReturnConstants.SUCCESS, false);
			result.put(ApiReturnConstants.ERROR, "实验报告id不能为空！");
			return new ResponseEntity(result, HttpStatus.OK);
		}
		if (StringUtils.isEmpty(domain.getContent())) {
			result = new HashMap();
			result.put(ApiReturnConstants.SUCCESS, false);
			result.put(ApiReturnConstants.ERROR, "实验报告内容不能为空！");
			return new ResponseEntity(result, HttpStatus.OK);
		}
		result = labReportService.save(domain);
		return new ResponseEntity(result, HttpStatus.OK);
	}

	@RequestMapping(value = "/review", method = RequestMethod.PUT, produces = MediaType.APPLICATION_JSON_VALUE)
	@ApiOperation(httpMethod = "PUT", value = "批阅实验报告", response = Void.class, notes = "批阅实验报告<br>@author zhengning")
	public ResponseEntity<?> review(
			@RequestHeader("Authorization") String token,
			@ApiParam(value = "id和reviewResult为必填项（批阅结果:pass-通过，notpass-未通过）", required = false) @RequestBody LabReportDomain domain) {
		AccountDTO account = accountService.getUserByToken(token);
		if (account == null) {
			return new ResponseEntity<Object>(TokenUtil.tokenValid(),
					HttpStatus.UNAUTHORIZED);
		}
		Map<String, Object> result = null;
		if (StringUtils.isEmpty(domain.getId())) {
			result = new HashMap();
			result.put(ApiReturnConstants.SUCCESS, false);
			result.put(ApiReturnConstants.ERROR, "实验报告id不能为空！");
			return new ResponseEntity(result, HttpStatus.OK);
		}
		if (StringUtils.isEmpty(domain.getReviewResult())) {
			result = new HashMap();
			result.put(ApiReturnConstants.SUCCESS, false);
			result.put(ApiReturnConstants.ERROR, "实验报告批阅结果不能为空！");
			return new ResponseEntity(result, HttpStatus.OK);
		}
		result = labReportService.review(domain);
		return new ResponseEntity(result, HttpStatus.OK);
	}

	@RequestMapping(value = "/chaperinit", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
	@ApiOperation(httpMethod = "GET", value = "初始化设置为实验章节下的班课实验报告", response = Void.class, notes = "初始化设置为实验章节下的班课实验报告<br>@author zhengning")
	public ResponseEntity<?> chaperInit(
			@RequestHeader("Authorization") String token,
			@ApiParam(value = "courseId课程id", required = false) @RequestParam(value = "courseId", required = true) String courseId,
			@ApiParam(value = "chaperId章节id", required = false) @RequestParam(value = "chaperId", required = true) Long chaperId) {
		AccountDTO account = accountService.getUserByToken(token);
		if (account == null) {
			return new ResponseEntity<Object>(TokenUtil.tokenValid(),
					HttpStatus.UNAUTHORIZED);
		}
		Map<String, Object> result = null;
		if (StringUtils.isEmpty(courseId)) {
			result = new HashMap();
			result.put(ApiReturnConstants.SUCCESS, false);
			result.put(ApiReturnConstants.ERROR, "课程id不能为空！");
			return new ResponseEntity(result, HttpStatus.OK);
		}

		if (StringUtils.isEmpty(chaperId)) {
			result = new HashMap();
			result.put(ApiReturnConstants.SUCCESS, false);
			result.put(ApiReturnConstants.ERROR, "章节id不能为空！");
			return new ResponseEntity(result, HttpStatus.OK);
		}
		labReportService.initChapterLabReport(account.getId(), courseId,
				chaperId);
		return new ResponseEntity(result, HttpStatus.OK);
	}

	@RequestMapping(value = "/courselist", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
	@ApiOperation(httpMethod = "GET", value = "课程列表", response = Void.class, notes = "课程列表<br>@author zhengning")
	public ResponseEntity<?> courseList(
			@RequestHeader("Authorization") String token) {
		AccountDTO account = accountService.getUserByToken(token);
		if (account == null) {
			return new ResponseEntity<Object>(TokenUtil.tokenValid(),
					HttpStatus.UNAUTHORIZED);
		}

		Map<String, Object> result = new HashMap<String, Object>();
		if (UserInforCore.ROLE_STUDENT.equals(account.getRole())) {
			result = labReportService.getStuCourseList(account.getId());
		} else {
			List<Course> list = courseService.findCourseByCreatedBy(account
					.getId());
			result.put(ApiReturnConstants.DATA, list);
			result.put(ApiReturnConstants.SUCCESS, true);
		}
		return new ResponseEntity(result, HttpStatus.OK);
	}

	@RequestMapping(value = "/chapterlist", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
	@ApiOperation(httpMethod = "GET", value = "实验章节列表", response = Void.class, notes = "实验章节列表<br>@author zhengning")
	public ResponseEntity<?> chapterList(
			@RequestHeader("Authorization") String token,
			@ApiParam(value = "courseId课程id", required = false) @RequestParam(value = "courseId", required = true) String courseId) {
		AccountDTO account = accountService.getUserByToken(token);
		if (account == null) {
			return new ResponseEntity<Object>(TokenUtil.tokenValid(),
					HttpStatus.UNAUTHORIZED);
		}
		Map<String, Object> result = new HashMap<String, Object>();
		List<CourseChapter> list = teacherChapterService
				.findCourseChapterForVm(courseId);
		result.put(ApiReturnConstants.DATA, list);
		result.put(ApiReturnConstants.SUCCESS, true);
		return new ResponseEntity(result, HttpStatus.OK);
	}

	@RequestMapping(value = "/courseclasslist", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
	@ApiOperation(httpMethod = "GET", value = "课程班级列表", response = Void.class, notes = "课程班级列表<br>@author zhengning")
	public ResponseEntity<?> courseClassList(
			@RequestHeader("Authorization") String token,
			@ApiParam(value = "courseId课程id", required = false) @RequestParam(value = "courseId", required = true) String courseId) {
		AccountDTO account = accountService.getUserByToken(token);
		if (account == null) {
			return new ResponseEntity<Object>(TokenUtil.tokenValid(),
					HttpStatus.UNAUTHORIZED);
		}
		Map<String, Object> result = new HashMap<String, Object>();
		List<CourseClassDTO> list = courseClassService.findCourseClassList("'"
				+ courseId + "'");
		result.put(ApiReturnConstants.DATA, list);
		result.put(ApiReturnConstants.SUCCESS, true);
		return new ResponseEntity(result, HttpStatus.OK);
	}

	@RequestMapping(value = "/findclasspage", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
	@ApiOperation(httpMethod = "POST", value = "分页查询班级实验报告提交情况", response = Void.class, notes = "分页查询班级实验报告提交情况<br>@author zhengning")
	public ResponseEntity<?> findClassPage(
			@RequestHeader("Authorization") String token,
			@ApiParam(value = "", required = false) @RequestBody QueryLabReportDomain domain) {
		AccountDTO account = accountService.getUserByToken(token);
		if (account == null) {
			return new ResponseEntity<Object>(TokenUtil.tokenValid(),
					HttpStatus.UNAUTHORIZED);
		}

		Map<String, Object> result = labReportService.findlabClassPage(domain);
		return new ResponseEntity(result, HttpStatus.OK);
	}

	@RequestMapping(value = "/findpage", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
	@ApiOperation(httpMethod = "POST", value = "分页查询学生实验报告提交情况", response = Void.class, notes = "分页查询学生实验报告提交情况<br>@author zhengning")
	public ResponseEntity<?> findPage(
			@RequestHeader("Authorization") String token,
			@ApiParam(value = "", required = false) @RequestBody QueryLabReportDomain domain) {
		AccountDTO account = accountService.getUserByToken(token);
		if (account == null) {
			return new ResponseEntity<Object>(TokenUtil.tokenValid(),
					HttpStatus.UNAUTHORIZED);
		}

		if (UserInforCore.ROLE_STUDENT.equals(account.getRole())) {
			domain.setStuId(account.getId());
		}else{
			domain.setTeacherId(account.getId());
		}
		Map<String, Object> result = labReportService.findlabStuPage(domain);
		return new ResponseEntity(result, HttpStatus.OK);
	}
	@RequestMapping(value = "/getClassReportList", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
	@ApiOperation(httpMethod = "POST", value = "获取班级报告提交情况", response = Void.class, notes = "获取班级报告提交情况<br>@author lwq")
	public ResponseEntity<?> getClassReportList(
			@RequestHeader("Authorization") String token,
			@ApiParam(value = "", required = false) @RequestBody QueryLabReportDomain domain) {
		AccountDTO account = accountService.getUserByToken(token);
		if (account == null) {
			return new ResponseEntity<Object>(TokenUtil.tokenValid(),
					HttpStatus.UNAUTHORIZED);
		}

		Map<String, Object> result = labReportService.getClassReportList(domain,account.getId());
		return new ResponseEntity(result, HttpStatus.OK);
	}
	@RequestMapping(value = "/getChapterReport", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
	@ApiOperation(httpMethod = "GET", value = "获取学生章节报告列表", response = Void.class, notes = "获取学生章节报告列表<br>@author lwq")
	public ResponseEntity<?> getChapterReport(
			@RequestHeader("Authorization") String token,
			@ApiParam(value = "courseClassId", required = true) @RequestParam String courseClassId,
			@ApiParam(value = "chapterId", required = true) @RequestParam Long chapterId,
			@ApiParam(value = "status", required = false) @RequestParam String status) {
		AccountDTO account = accountService.getUserByToken(token);
		if (account == null) {
			return new ResponseEntity<Object>(TokenUtil.tokenValid(),
					HttpStatus.UNAUTHORIZED);
		}
		List<ReportDomain> chapterReport = labReportService.getChapterReport(chapterId, courseClassId,status);
		return new ResponseEntity(chapterReport, HttpStatus.OK);
	}
	@RequestMapping(value = "/getChapterReportInfo", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
	@ApiOperation(httpMethod = "GET", value = "获取章节报告详情", response = Void.class, notes = "获取章节报告详情<br>@author lwq")
	public ResponseEntity<?> getChapterReportInfo(
			@RequestHeader("Authorization") String token,
			@ApiParam(value = "courseClassId", required = false) @RequestParam String courseClassId,
			@ApiParam(value = "chapterId", required = false) @RequestParam Long chapterId) {
		AccountDTO account = accountService.getUserByToken(token);
		if (account == null) {
			return new ResponseEntity<Object>(TokenUtil.tokenValid(),
					HttpStatus.UNAUTHORIZED);
		}
		ClassReportDomain report = labReportService.getReport(chapterId, courseClassId);
		return new ResponseEntity(report, HttpStatus.OK);
	}
	@RequestMapping(value = "/getReportInfo", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
	@ApiOperation(httpMethod = "GET", value = "获取报告详情", response = Void.class, notes = "获取报告详情<br>@author lwq")
	public ResponseEntity<?> getReportInfo(
			@RequestHeader("Authorization") String token,
			@ApiParam(value = "id", required = false) @RequestParam String id) {
		AccountDTO account = accountService.getUserByToken(token);
		if (account == null) {
			return new ResponseEntity<Object>(TokenUtil.tokenValid(),
					HttpStatus.UNAUTHORIZED);
		}
		LabReportInfoDomain labReport = labReportService.getLabReport(id);
		return new ResponseEntity(labReport, HttpStatus.OK);
	}
}
